package com.situ.personal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.personal.dao.UserRoleDao;
import com.situ.personal.entity.PageInfo;

public class UserRoleService {
	UserRoleDao userRoleDao = new UserRoleDao();

	public PageInfo findByPage(int pageNo, int pageSize) {
		PageInfo pageInfo = new PageInfo();
		
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
		
		// totalPage
		int count = userRoleDao.getTotalCount();
		int totalPage = (int)Math.ceil( (double)count / pageSize);
		pageInfo.setTotalPage(totalPage);
				
		//list
		int offset = (pageNo-1)*pageSize;
		List<Map<String,Object>> list = userRoleDao.fandPageInfo(offset,pageSize);
		pageInfo.setList(list);
		return pageInfo;
	}

	public boolean insert(Integer userId, ArrayList<Integer> roleIdsList) {
		boolean isSuccess = false;
		
		// roleIdsList是集合    存放了   多个roleId,,,   每遍历一次拿到1个roleId,,,,,调一次Dao,,最后取出来x个roleId，就调多少次Dao
		for(Integer roleId : roleIdsList){
			isSuccess = userRoleDao.insert(userId,roleId);
		}
		return isSuccess;
	}

	public boolean deleteById(int userId, int roleId) {
		boolean isSccuess = userRoleDao.deleteById(userId,roleId);
		return isSccuess;
	}
}
